package com.dbf.forum.common.utils;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.dbf.forum.user.model.User;


public class SecurityHelper {
	
	public static User currentUser() {
        Authentication auth = SecurityContextHolder.getContext()
                .getAuthentication();
        if (auth == null) {
            return null;
        }

        if (auth instanceof AnonymousAuthenticationToken) {
            return null;
        }

        return (User) auth.getPrincipal();
    }
	
	public static Long currentUserId(){
		User user = currentUser();
		return user == null ? null : user.getId(); 
	}

}
